﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using KinonekoSoftware.UI;

namespace KinonekoSoftware.Studio.Controls
{
    public sealed class BorderEx : Border
    {
        private static readonly DashStyle _dash = new DashStyle(new double[] { 2 }, 4);
        private                 Pen       _solidPen;
        private                 Pen       _dottedPen;
        
        protected override void OnRender(DrawingContext dc)
        {
            var margin = Math.Clamp(LineMargin, 16, 512);
            var y      = ActualHeight;
            Pen p;

            //
            // 先渲染
            base.OnRender(dc);

            if (IsDottedLine)
            {
                _dottedPen ??= new Pen()
                {
                    Brush     = new SolidColorBrush(Colors.DarkGray)
                    {
                        Opacity = 0.6,
                    },
                    Thickness = 2,
                    DashStyle = _dash,                   
                };
                p = _dottedPen;
            }
            else
            {
                
                _solidPen ??= new Pen(new SolidColorBrush(Colors.DarkGray), 2);
                p = _solidPen;
            }
            
            dc.DrawLine(p, new Point(margin, 0), new Point(margin, y));
        }
        
        

        public static readonly DependencyProperty LineMarginProperty = DependencyProperty.Register(
            nameof(LineMargin),
            typeof(double),
            typeof(BorderEx),
            new FrameworkPropertyMetadata(64d, FrameworkPropertyMetadataOptions.AffectsRender));


        public static readonly DependencyProperty IsDottedLineProperty = DependencyProperty.Register(
            nameof(IsDottedLine),
            typeof(bool),
            typeof(BorderEx),
            new FrameworkPropertyMetadata(Boxing.False, FrameworkPropertyMetadataOptions.AffectsRender));

        public bool IsDottedLine
        {
            get => (bool)GetValue(IsDottedLineProperty);
            set => SetValue(IsDottedLineProperty, Boxing.Box(value));
        }

        public double LineMargin
        {
            get => (double)GetValue(LineMarginProperty);
            set => SetValue(LineMarginProperty, value);
        }
    }
}